#!/bin/bash

# CentOS 7 环境检查脚本
# 用于验证CentOS 7环境中部署酒店管理系统所需的依赖是否已正确安装

echo "检查CentOS 7酒店管理系统部署环境..."

# 检查操作系统版本
echo "检查操作系统版本..."
if [[ -f /etc/redhat-release ]]; then
    OS_VERSION=$(cat /etc/redhat-release)
    echo "  操作系统: $OS_VERSION"
    if [[ $OS_VERSION == *"CentOS Linux release 7."* ]]; then
        echo "  ✓ 操作系统版本兼容"
    else
        echo "  ! 警告: 可能不完全兼容的CentOS版本"
    fi
else
    echo "  ! 未检测到CentOS系统"
fi

# 检查系统资源
echo "检查系统资源..."
MEMORY_TOTAL=$(free -m | awk '/^Mem:/{print $2}')
DISK_AVAILABLE=$(df . | awk 'NR==2 {print $4}')
echo "  总内存: ${MEMORY_TOTAL}MB"
if [ $MEMORY_TOTAL -lt 2048 ]; then
    echo "  ! 警告: 内存不足2GB，可能影响性能"
else
    echo "  ✓ 内存满足要求"
fi
echo "  可用磁盘空间: ${DISK_AVAILABLE}KB"
if [ $DISK_AVAILABLE -lt 10485760 ]; then  # 10GB in KB
    echo "  ! 警告: 可用磁盘空间不足10GB"
else
    echo "  ✓ 磁盘空间满足要求"
fi

# 检查Docker
echo "检查Docker..."
if command -v docker &> /dev/null; then
    DOCKER_VERSION=$(docker --version)
    echo "  Docker版本: $DOCKER_VERSION"
    echo "  ✓ Docker已安装"
    
    # 检查Docker服务状态
    if systemctl is-active --quiet docker; then
        echo "  ✓ Docker服务正在运行"
    else
        echo "  ✗ Docker服务未运行，请执行: sudo systemctl start docker"
    fi
else
    echo "  ✗ Docker未安装"
    echo "    安装命令:"
    echo "      sudo yum install -y yum-utils device-mapper-persistent-data lvm2"
    echo "      sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo"
    echo "      sudo yum install -y docker-ce docker-ce-cli containerd.io"
    echo "      sudo systemctl start docker"
    echo "      sudo systemctl enable docker"
fi

# 检查Docker Compose
echo "检查Docker Compose..."
if command -v docker-compose &> /dev/null; then
    DOCKER_COMPOSE_VERSION=$(docker-compose --version)
    echo "  Docker Compose版本: $DOCKER_COMPOSE_VERSION"
    echo "  ✓ Docker Compose已安装"
else
    echo "  ✗ Docker Compose未安装"
    echo "    安装命令:"
    echo "      sudo curl -L \"https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose"
    echo "      sudo chmod +x /usr/local/bin/docker-compose"
    echo "      sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose"
fi

# 检查Git
echo "检查Git..."
if command -v git &> /dev/null; then
    GIT_VERSION=$(git --version)
    echo "  Git版本: $GIT_VERSION"
    echo "  ✓ Git已安装"
else
    echo "  ✗ Git未安装，请执行: sudo yum install -y git"
fi

# 检查端口占用
echo "检查端口占用情况..."
PORTS=(80 3306 5000)
for PORT in "${PORTS[@]}"; do
    if netstat -tlnp | grep -q ":$PORT "; then
        PROCESS=$(netstat -tlnp | grep ":$PORT " | awk '{print $7}')
        echo "  ! 端口 $PORT 已被占用 ($PROCESS)"
    else
        echo "  ✓ 端口 $PORT 未被占用"
    fi
done

# 检查当前目录权限
echo "检查目录权限..."
if [ -w . ]; then
    echo "  ✓ 当前目录具有写权限"
else
    echo "  ✗ 当前目录没有写权限"
fi

# 检查必要文件
echo "检查必要文件..."
REQUIRED_FILES=("docker-compose.yml" "Dockerfile" "app.py" "requirements.txt" "docker-init-db.py")
for FILE in "${REQUIRED_FILES[@]}"; do
    if [ -f "$FILE" ]; then
        echo "  ✓ $FILE 存在"
    else
        echo "  ✗ $FILE 不存在"
    fi
done

# 检查用户权限
echo "检查用户权限..."
if groups $USER | grep -q docker; then
    echo "  ✓ 用户在docker组中"
else
    echo "  ! 用户不在docker组中，可能需要sudo权限运行docker命令"
    echo "    添加用户到docker组命令: sudo usermod -aG docker $USER"
fi

echo ""
echo "环境检查完成。"
echo "请确保所有标记为✗的项目都已正确安装和配置后再进行部署。"